#include "Periferial.h"

void Periferial(void)
{
	SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);

	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
	//
    // Configure push button.
    //
	GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, PUSH_BUTTON);
	GPIOIntTypeSet(GPIO_PORTC_BASE, PUSH_BUTTON , GPIO_FALLING_EDGE );
	GPIOPinIntEnable(GPIO_PORTC_BASE, PUSH_BUTTON);
	IntEnable(INT_GPIOC);
	//
    // Configure the ADC to sample the potentiometer when the timer expires.
    //
    ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC_BASE, 3, 0, ADC_CTL_CH0 |  ADC_CTL_IE| ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 3);
    ADCIntEnable(ADC_BASE, 3);
    IntEnable(INT_ADC3);
	//
    // Configure the two 32-bit periodic timers.
    //
    TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PERIODIC);
    TimerLoadSet(TIMER0_BASE, TIMER_B, (SysCtlClockGet() / 1000) - 1);

	TimerConfigure(TIMER2_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PERIODIC);
    TimerLoadSet(TIMER2_BASE, TIMER_B, SysCtlClockGet());

    TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER);
    TimerLoadSet(TIMER1_BASE, TIMER_A, SysCtlClockGet()/ 8 );
	TimerControlStall(TIMER1_BASE, TIMER_A , true );
    TimerControlTrigger(TIMER1_BASE, TIMER_A, true);

	//
    // Setup the interrupts for the timer timeouts.
    //
    TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
    IntEnable(INT_TIMER0B);
	TimerIntEnable(TIMER2_BASE, TIMER_TIMB_TIMEOUT);
    IntEnable(INT_TIMER2B);

	OSRAM96x16x1Init(true);
    //
    // Enable the timer.
    //
    TimerEnable(TIMER1_BASE, TIMER_A);
	TimerEnable(TIMER0_BASE, TIMER_B);
	TimerEnable(TIMER2_BASE, TIMER_B);	 
	//
    // Enable processor interrupts.
    //
    IntMasterEnable(); 	
}
